﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnTheWay.Interfaces;
using Models;
using BlackMofo.BLL.Interfaces;
using FileService;

namespace BlackMofo.BLL.Impls
{
    /// <summary>
    /// ExhibitType对象实现
    /// </summary>
    public class ExhibitTypeBLLImpl : BLLImpl<ExhibitType>, IExhibitTypeBLL { }    
    /// <summary>
    /// SysAdmin对象实现
    /// </summary>
    public class SysAdminBLLImpl : BLLImpl<SysAdmin>, ISysAdminBLL { }
    /// <summary>
    /// User对象实现
    /// </summary>
    public class UserBLLImpl : BLLImpl<User>, IUserBLL { }
    /// <summary>
    /// ExhibitUnit对象实现
    /// </summary>
    public class ExhibitUnitBLLImpl : BLLImpl<ExhibitUnit>, IExhibitUnitBLL
    {
        public override bool DeleteByID(Guid authid, Guid id)
        {
            ExhibitUnit model = GetByID(authid, id);
            if (model != null)
            {
                return Delete(authid, model);
            }
            return false;
            //return base.DeleteByID(authid, id);
        }

        public override bool Delete(Guid authid, ExhibitUnit model)
        {
            List<String> ltname = GetDeleteImage(new ExhibitUnit[] { model });
            List<String> ltvideo = GetDeleteVideo(new ExhibitUnit[] { model });
            if (base.Delete(authid, model))
            {
                DeleteFileHelper.DeleteFileImage(ltname);
                DeleteFileHelper.DeleteFileVideo(ltvideo);
                return true;
            }
            return false;
            //return base.Delete(authid, model);
        }

        public override bool DeleteList(Guid authid, IList<ExhibitUnit> list)
        {
            if (list == null || list.Count == 0)
            {
                return true;
            }
            List<String> ltname = GetDeleteImage(list);
            List<String> ltvideo = GetDeleteVideo(list);
            if (base.DeleteList(authid, list))
            {
                DeleteFileHelper.DeleteFileImage(ltname);
                DeleteFileHelper.DeleteFileVideo(ltvideo);
                return true;
            }
            return false;
            //return base.DeleteList(authid, list);
        }

        public override bool Update(Guid authid, ExhibitUnit model)
        {
            if (base.Update(authid, model))
            {
                this.MoveFiles(model);
                return true;
            }
            return false;
            //return base.Update(authid, model);
        }
        /// <summary>
        /// 忽略图片文件处理
        /// </summary>
        /// <param name="authid"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public override bool UpdateList(Guid authid, IList<ExhibitUnit> list)
        {
            return base.UpdateList(authid, list);
        }

        public override bool Insert(Guid authid, ExhibitUnit model)
        {
            if (base.Insert(authid, model))
            {
                this.MoveFiles(model);
                return true;
            }
            return false;
            //return base.Insert(authid, model);
        }

        internal void MoveFiles(ExhibitUnit model)
        {
            try
            {
                FileFactory.FileOperate(OPFileType.MOVETO, FolderEnum.TEMP, model.MooMaUrl, FolderEnum.IMAGE, model.MooMaUrl);
                if (!String.IsNullOrWhiteSpace(model.UnitUrls))
                {
                    string[] names = model.UnitUrls.Split(',');
                    foreach (var name in names)
                    {
                        if (model.TypeEnum == ExhibitUnitTypeEnum.视频)
                        {
                            FileFactory.FileOperate(OPFileType.MOVETO, FolderEnum.TEMP, name, FolderEnum.VIDEO, name);
                        }
                        else
                        {
                            FileFactory.FileOperate(OPFileType.MOVETO, FolderEnum.TEMP, name, FolderEnum.IMAGE, name);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        internal List<string> GetDeleteImage(IEnumerable<ExhibitUnit> list)
        {
            List<string> ltname = new List<string>();
            foreach (var item in list)
            {
                ltname.Add(item.MooMaUrl);
                if (!String.IsNullOrWhiteSpace(item.UnitUrls))
                {
                    string[] names = item.UnitUrls.Split(',');
                    foreach (var name in names)
                    {
                        ltname.Add(name);
                    }
                }
            }
            return ltname;
        }

        internal List<string> GetDeleteVideo(IEnumerable<ExhibitUnit> list)
        {
            List<string> ltname = new List<string>();
            foreach (var item in list)
            {
                if (item.TypeEnum == ExhibitUnitTypeEnum.视频 && !String.IsNullOrWhiteSpace(item.UnitUrls))
                {
                    string[] names = item.UnitUrls.Split(',');
                    foreach (var name in names)
                    {
                        ltname.Add(name);
                    }
                }
            }
            return ltname;
        }
    }
    /// <summary>
    /// Exhibit对象实现
    /// </summary>
    public class ExhibitBLLImpl : BLLImpl<Exhibit>, IExhibitBLL
    {
        public override bool DeleteByID(Guid authid, Guid id)
        {
            Exhibit model = GetByID(authid, id);
            if (model != null)
            {
                return Delete(authid, model);
            }
            return false;
            //return base.DeleteByID(authid, id);
        }

        public override bool Delete(Guid authid, Exhibit model)
        {
            string where = " where ExhibitID='" + model.ID + "'";
            List<String> ltname = GetDeleteFile(new Exhibit[] { model });
            IList<ExhibitUnit> ltunit = BLLFactory.ExhibitUnitBLL.GetListWhere(authid,where, "", "");
            if (BLLFactory.ExhibitUnitBLL.DeleteList(authid,ltunit) && base.Delete(authid, model))
            {
                DeleteFileHelper.DeleteFileImage(ltname);
                return true;
            }
            return false;
            //return base.Delete(authid, model);
        }

        public override bool DeleteList(Guid authid, IList<Exhibit> list)
        {
            if (list.Count == 0)
            {
                return false;
            }
            string where = " where ExhibitID in ('" + list[0].ID;
            for (int i = 1; i < list.Count; i++)
            {
                where += "','" + list[i].ID;
            }
            where += "')";
            List<String> ltname = GetDeleteFile(list);
            IList<ExhibitUnit> ltunit = BLLFactory.ExhibitUnitBLL.GetListWhere(authid, where, "", "");
            if (BLLFactory.ExhibitUnitBLL.DeleteList(authid, ltunit) && base.DeleteList(authid, list))
            {
                DeleteFileHelper.DeleteFileImage(ltname);
                return true;
            }
            return false;
            //return base.DeleteList(authid, list);
        }

        public override bool Update(Guid authid, Exhibit model)
        {
            if (base.Update(authid, model))
            {
                try
                {
                    FileFactory.FileOperate(OPFileType.MOVETO, FolderEnum.TEMP, model.MooMaUrl, FolderEnum.IMAGE, model.MooMaUrl);
                }
                catch (Exception)
                {
                }
                return true;
            }
            return false;
            //return base.Update(authid, model);
        }
        /// <summary>
        /// 忽略封面文件处理
        /// </summary>
        /// <param name="authid"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public override bool UpdateList(Guid authid, IList<Exhibit> list)
        {
            return base.UpdateList(authid, list);
        }

        public override bool Insert(Guid authid, Exhibit model)
        {
            if (base.Insert(authid, model))
            {
                try
                {
                    FileFactory.FileOperate(OPFileType.MOVETO, FolderEnum.TEMP, model.MooMaUrl, FolderEnum.IMAGE, model.MooMaUrl);
                }
                catch (Exception)
                {
                }
                return true;
            }
            return false;
            //return base.Insert(authid, model);
        }

        internal List<string> GetDeleteFile(IEnumerable<Exhibit> list)
        {
            List<string> ltname = new List<string>();
            foreach (var item in list)
            {
                ltname.Add(item.MooMaUrl);
            }
            return ltname;
        }
    }

    public class DeleteFileHelper
    {
        public static void DeleteFile(IEnumerable<String> ltname, int folder)
        {
            try
            {
                foreach (var item in ltname)
                {
                    FileFactory.Delete(folder, item);
                }
            }
            catch (Exception)
            {
            }
        }
        public static void DeleteFileImage(IEnumerable<String> ltname)
        {
            try
            {
                foreach (var item in ltname)
                {
                    FileFactory.Delete(FolderEnum.IMAGE, item);
                }
            }
            catch (Exception)
            {
            }
        }
        public static void DeleteFileVideo(IEnumerable<String> ltname)
        {
            try
            {
                foreach (var item in ltname)
                {
                    FileFactory.Delete(FolderEnum.VIDEO, item);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
